/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package networkresourcescanner;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.swing.JTree;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author HIRAYAMI
 */
public class W {

    static final Object isWindowLocked = new Object();

    public static void P(Object obj) {
        System.out.println(obj);
    }

    public static void PA(Object[] obj) {
        P("Printing Array: size(" + obj.length + ") :" + obj);
        for (int i = 0; i < obj.length; i++) {
            //P(obj[i] + ":" + obj[i].getClass());
            P(obj[i]);
        }
    }

    public static void exec(String command) {
        try {
            P("Executing command:\n\t" + command);
            Process p = Runtime.getRuntime().exec(command);
            //p.waitFor();
            printExecOutput(p);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public static void execSingle(String[] commandGroup){
        try {
            P("Executing command:");
            PA(commandGroup);
            Process p = Runtime.getRuntime().exec(commandGroup);
            //p.waitFor();
            printExecOutput(p);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void exec(String[] commands) {
        for (int i = 0; i < commands.length; i++) {
            exec(commands[i]);
        }
    }

    public static String printExecOutput(Process p) {
        String s, result = "";
        int count = 0;
        try {
            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((s = stdInput.readLine()) != null) {
                count++;
                result = result + s + "\n";
                System.out.println("Output:" + count + "\n" + result);
            }
            stdInput.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

    public static void loadConfiguration() {
    }
    
    
    public static void expandAll(JTree tree, TreePath parent, boolean expand) {
        // Traverse children
        TreeNode node = (TreeNode) parent.getLastPathComponent();
        if (node.getChildCount() >= 0) {
            for (Enumeration e = node.children(); e.hasMoreElements();) {
                TreeNode n = (TreeNode) e.nextElement();
                TreePath path = parent.pathByAddingChild(n);
                expandAll(tree, path, expand);
            }
        }

        // Expansion or collapse must be done bottom-up
        if (expand) {
            tree.expandPath(parent);
        } else {
            tree.collapsePath(parent);
        }
    }
}
